package com.emip.servlet.analysis;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.emip.bean.DayStoreBean;
import com.emip.pojo.Report;
import com.emip.pojo.Table;
import com.emip.service.WeekStoreService;
import com.emip.service.MonthStoreService;
import com.emip.service.QuarterStoreService;
import com.emip.service.YearStoreService;
import com.emip.util.TableFormat;


@Controller
@RequestMapping(value="/analysis/total")
public class TotalAction {

	@Autowired
	WeekStoreService weekStoreService;
	@Autowired
	MonthStoreService monthStoreService;
	@Autowired
	QuarterStoreService quarterStoreService;
	@Autowired
	YearStoreService yearStoreService;

	protected ModelMap setDefaultParams(ModelMap mmap) {
		if (mmap == null) mmap = new ModelMap();
		
		return mmap;
	}
	
	@RequestMapping(value="/show")
	public ModelAndView index() {
		return new ModelAndView("/analysis/total", setDefaultParams(mmap));
	}
	
	@RequestMapping(value="/query", method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> query(
			@RequestParam("startDate") String startDate, 
			@RequestParam("siteIds[]") String siteId,
			@RequestParam("itemIds[]") String[] itemIds,
			@RequestParam("timespan") int timespan) {
		List result = null;
		switch (timespan) {
			case "year":
				result = yearStoreService.getByTimespan(startDate, startDate, itemIds, siteId);
				break;
			case "month":
				result = monthStoreService.getByTimespan(startDate, startDate, itemIds, siteId);
				break;
			default:
				result = weekStoreService.getByTimespan(startDate, startDate, itemIds, siteId);
				break;
		}
		Report report = formatReportBody(result);
		return report.getBodys();
	}
}
